﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace visu.Controllers
{
    public class MonitorController : Controller
    {
        //
        // GET: /Monitor/

        public ActionResult MonitorListInfo()
        {
            return View();
        }
        public ActionResult MonitorListInfoDetail(int page, int rows, materialsummary materialsummary,string Year,string Month, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "M_CODE" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "SUMMARY_TYPE", Operator = "=", Value = "0" };
            SearchCondition condition1 = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);
            listSearch.Add(condition1);

            if(Year!="")
            {
                SearchCondition conditionyear = new SearchCondition() { Field = "ACCOUNTYEAR", Operator = "=", Value = Year };
                listSearch.Add(conditionyear);
            }
            if (Month != "")
            {
                SearchCondition conditionmonth = new SearchCondition() { Field = "ACCOUNTMONTH", Operator = "=", Value = Month };
                listSearch.Add(conditionmonth);
            }

            Expression<Func<materialsummary, bool>> exp = SearchExpression.GetSearchExpression<materialsummary>(materialsummary, listSearch, SearchContains, "M_CODE", "M_NAME", "M_SPECIFICATION", "M_TYPE");

            var list = BLLSession.Current.materialsummaryBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.materialsummaryDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }


        public ActionResult MonitorMachineListInfo()
        {
            return View();
        }
        public ActionResult MonitorMachineListInfoDetail(int page, int rows, materialsummary materialsummary, string Year, string Month, string Machinename, string SearchContains, string sort, string order)
        {
            sort = string.IsNullOrEmpty(sort) ? "M_CODE" : sort;
            order = string.IsNullOrEmpty(order) ? "asc" : order;

            SearchCondition condition = new SearchCondition() { Field = "SUMMARY_TYPE", Operator = "=", Value = "1" };
            SearchCondition condition1 = new SearchCondition() { Field = "COMPANY_ID", Operator = "=", Value = OperateContext.userInfo.COMPANY_ID.ToString() };
            List<SearchCondition> listSearch = new List<SearchCondition>();
            listSearch.Add(condition);
            listSearch.Add(condition1);

            if (Year != "")
            {
                SearchCondition conditionyear = new SearchCondition() { Field = "ACCOUNTYEAR", Operator = "=", Value = Year };
                listSearch.Add(conditionyear);
            }
            if (Month != "")
            {
                SearchCondition conditionmonth = new SearchCondition() { Field = "ACCOUNTMONTH", Operator = "=", Value = Month };
                listSearch.Add(conditionmonth);
            }
            if (Machinename != "")
            {
                SearchCondition conditionmachinename = new SearchCondition() { Field = "MACHINENAME", Operator = "co", Value = Machinename };
                listSearch.Add(conditionmachinename);
            }
            Expression<Func<materialsummary, bool>> exp = SearchExpression.GetSearchExpression<materialsummary>(null, listSearch, SearchContains, "M_CODE", "M_NAME", "M_SPECIFICATION", "M_TYPE");

            var list = BLLSession.Current.materialsummaryBLL.GetPagedList(page, rows, exp, sort, order);
            int count = DALSession.Current.materialsummaryDAL.GetCount(exp);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "", total = count, rows = list };

            return Json(dataPager, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UpdateMaterialSummary(string summaryType)
        {
            MaterialSummary(summaryType);
            var tempObj = new { code = "success", data = "", message = "更新成功！" };
            return Json(tempObj, JsonRequestBehavior.AllowGet);
        }

        protected void MaterialSummary(string summaryType)
        {
            SqlConnection sqlConn = new SqlConnection();
            sqlConn.ConnectionString = DBCallCommon.GetStringValue("connectionStrings");
            DBCallCommon.openConn(sqlConn);
            SqlCommand sqlCmd = new SqlCommand("PRO_MaterialOutSummary", sqlConn);
            sqlCmd.CommandTimeout = 300;
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.Add("@summary_type", SqlDbType.Int, 4);
            sqlCmd.Parameters["@summary_type"].Value = summaryType;
            sqlCmd.ExecuteNonQuery();
            DBCallCommon.closeConn(sqlConn);
        }

    }
}
